package com.itboye.sunsun.luntan;

import java.util.ArrayList;
import java.util.List;

import com.itboye.sunsunhome.R;
import com.itboye.sunsun.adapter.TieZiSouSuoAdapter;
import com.itboye.sunsun.beans.TieZiSouSuoBean;
import com.itboye.sunsun.beans.LunTanHomePageBean.LunTanItemBean;
import com.itboye.sunsun.beans.TieZiSouSuoBean.SouSuoItemBean;
import com.itboye.sunsun.custome.XListView;
import com.itboye.sunsun.network.HttpRequest;
import com.itboye.sunsun.support.BaseActivity;
import com.itboye.sunsun.volley.MyJsonRequest;
import com.itboye.sunsun.volley.XErrorListener;
import com.itboye.sunsun.volley.XRequestListener;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import u.aly.da;

public class LunTanSouSuoActivity extends BaseActivity {

	ViewGroup imgContainer;
	TextView shenhyu;

	EditText input;

	XListView xlistview;

	BaseAdapter adapter;
	List<SouSuoItemBean> dataList = new ArrayList<>();

	@Override
	protected void onCreate(Bundle arg0) {

		super.onCreate(arg0);

		setContentView(R.layout.activity_luntan_sousuo);

		setStatusBarColor(R.color.top_blue);
		initView();
	}

	private void initView() {

		adapter = new TieZiSouSuoAdapter(dataList);
		xlistview.setAdapter(adapter);
		findViewById(R.id.back).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				finish();
			}
		});

		input.setOnEditorActionListener(new OnEditorActionListener() {

			@Override
			public boolean onEditorAction(TextView v, int actionId,
					KeyEvent event) {
				if (actionId == EditorInfo.IME_ACTION_SEARCH) {
					// 先隐藏键盘
					((InputMethodManager) input.getContext().getSystemService(
							Context.INPUT_METHOD_SERVICE))
							.hideSoftInputFromWindow(getCurrentFocus()
									.getWindowToken(),
									InputMethodManager.HIDE_NOT_ALWAYS);

					getData();
					return true;
				}
				return false;
			}
		});

		xlistview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				position--;

				Intent intent = new Intent(getApplicationContext(),
						TieZiDetailActivity.class);
				SouSuoItemBean bean = dataList.get(position);
				intent.putExtra("fid", bean.getFid());
				intent.putExtra("tid", bean.getTid());

				intent.putExtra("author", bean.getAuthor());
				intent.putExtra("author_id", bean.getAuthorId());
				intent.putStringArrayListExtra("img", bean.getImg());
				startActivity(intent);

			}
		});

	}

	protected void getData() {
		MyJsonRequest<TieZiSouSuoBean> request = new MyJsonRequest.Builder<TieZiSouSuoBean>()
				.typeKey("BY_ForumThread_query").apiVer("100")
				.param("name", input.getText().toString())
				.requestListener(new XRequestListener<TieZiSouSuoBean>() {

					@Override
					public void onResponse(TieZiSouSuoBean arg0) {
						dataList.clear();
						if (arg0.getCount() > 0) {
							dataList.addAll(arg0.getList());
							adapter.notifyDataSetChanged();
						}

					}
				}).errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code,
							String msg) {

					}
				}).build();

		HttpRequest.getDefaultRequestQueue().add(request);
	}

}
